package com.bootcamp.servlet.mvc.utils;

import com.bootcamp.servlet.mvc.model.Product;
import com.bootcamp.servlet.util.AuthLoginUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;

/**
 * @program: wrok-1101
 * @description:
 * @author: Keyu Li
 * @create: 2021-11-08 14:21
 **/

public class ShopFootprintUtils {
    public static void printFootprint(HttpServletRequest request, HttpServletResponse response) throws IOException {
        HttpSession session = request.getSession();
        Map footprintMap = (Map) session.getAttribute("footprintMap");
        Deque<Integer> footprintDeque = (Deque)session.getAttribute("footprintDeque");
        PrintWriter writer = response.getWriter();
        // 如果历史记录为空，则返回购物页面
        if (footprintDeque==null || footprintMap==null || footprintDeque.size()==0) {
            writer.println("历史记录为空！");
            return;
        }

        // 展示历史记录及访问时间
        List<Product> products = (List<Product>) request.getServletContext().getAttribute("products");
        writer.println("<div><table>");
        // 第一行
        writer.println("        <tr style=\"font-size: larger\">\n" +
                "            <td>品牌</td>\n" +
                "            <td>颜色</td>\n" +
                "            <td>类别</td>\n" +
                "            <td>价格</td>\n" +
                "            <td>描述</td>\n" +
                "            <td>详情</td>\n" +
                "            <td>上次浏览(5条)</td>\n" +
                "            <td></td>\n" +
                "        </tr>");
        for (Integer id : footprintDeque) {
            for (Product p : products) {
                if (p.getId().equals(id)){
                    // 展示商品
                    // 输出商品详情
                    String detailhref = request.getContextPath() + "/store/detail?id=" + p.getId();
                    String footprintDate = footprintMap.get(id).toString();
                    writer.println("<tr>\n" +
                            "                <td>" + p.getName() + "</td>\n" +
                            "                <td>" + p.getColor() + "</td>\n" +
                            "                <td>" + p.getCategory() + "</td>\n" +
                            "                <td>" + p.getPrice() + "</td>\n" +
                            "                <td>" + p.getDescription() + "</td>\n" +
                            "                <td><a href='" + detailhref + "'>详情</a></td>\n" +
                            "                <td>" + footprintDate + "</td>\n" +
                            "        </tr>");
                }
            }
        }
        writer.println("<div><table>");
    }

    public static void addFootPrint(HttpSession session, Integer id){
        //能够存储的历史记录总数为5，显示最近数为2
        final Integer ALL_FOOT = 5;
        final Integer RECENT = 2;
        Map footprintMap = (Map) session.getAttribute("footprintMap");
        Deque footprintDeque = (Deque) session.getAttribute("footprintDeque");
        if (footprintMap == null || footprintDeque == null) {
            footprintMap = new HashMap<Integer, Date>();
            footprintDeque = new LinkedList();
            session.setAttribute("footprintMap", footprintMap);
            session.setAttribute("footprintDeque", footprintDeque);
        }
        // 如果记录中已经包含，deque中删除旧记录，在尾部插入新记录
        if (footprintMap.get(id) != null) {
            footprintDeque.remove(id);
        }
        footprintMap.put(id, new Date());
        footprintDeque.addFirst(id);
        // 如果容量超过限制，Deque删除头部节点，Map中也删除对应key
        if (footprintDeque.size() > ALL_FOOT) {
            Integer last = (Integer) footprintDeque.removeLast();
            footprintMap.remove(last);
        }
        return;
    }
}
